/* all the config section functions are here */
function confirm_truncate(uri)
{
	if(confirm('Are you sure you want to truncate the movies? This will remove all movie data.\nIt is suggested you make a backup first.')) {
		_redirect(uri);
	}
}

function confirm_cleanup(uri)
{
	if(confirm('Are you sure you want to remove all un-used movies? It is suggested you make a backup first')) {
		_redirect(uri);
	}
}

function confirm_update(uri)
{
	if(confirm('Are you sure you want to update all your movies from imdb. This may take a VERY long time depending on the size of your movie database')) {
		_redirect(uri);
	}
}

/* start update movie database ajax */
var update_counter	= 0;
var update_paused	= false;
var update_total	= 0;
function update_start()
{
	document.getElementById('loading').style.visibility = 'visible';
	ajax_http_request('index.php?what=config&do=process_update&c='+update_counter, 'update_process');
}

function update_pause()
{
	if(update_paused) {
		// start
		update_paused = false;
		update_counter++;
		update_start();
		document.getElementById('pause').value = 'Pause';
	} else {
		// pause
		update_paused = true;
		pause = document.getElementById('pause');
		pause.disabled = true;
		pause.blur();
	}

}

// update the percentage bar
function update_percentage()
{
	bar = document.getElementById('bar');

	par = bar.parentNode;
	full_width = par.clientWidth - 4;

	pix = (update_counter / update_total * full_width);
	bar.style.width = pix + 'px';

}

function update_process(data)
{
	data = data.split('|');
	results = document.getElementById('results');
	// && update_counter < 5
	if(data[0] != 'finished') {
		// this movie has been successfully updated

		// trim title
		title = data[3];
		if(title == '') {
			alert('Empty Title: #'+data[1]);
		}
		if(title.length > 15) {
			title = title.substr(0, 20) + '...';
		}

		// create new element
		li = document.createElement('LI');
		li.innerHTML = '<span title="'+ data[3] +'">'+ title + '</span> (' + data[4] + ') - ';

		// status
		if(data[0] == 'success') {
			li.innerHTML += '<span class="success">Success</span>';
		} else {
			li.innerHTML += '<span class="failed">Failed</span>';
		}

		// alternate the colours
		if(update_counter % 2 == 0) {
			li.setAttribute('class', 'even');
		} else {
			li.setAttribute('class', 'odd');
		}

		results.appendChild(li);

		// scroll to bottom
		results.scrollTop = results.scrollHeight;

		// call the next movie
		if(update_paused == false) {
			update_counter++;
			update_percentage();
			ajax_http_request('index.php?what=config&do=process_update&c='+update_counter, 'update_process');
		} else {
			document.getElementById('loading').style.visibility = '';
			pause = document.getElementById('pause');
			pause.value = 'Start';
			pause.disabled = false;
		}

	} else {
		// we are finished, enable the continue button
		document.getElementById('loading').style.visibility = '';
		document.getElementById('pause').disabled = true;
		cont = document.getElementById('continue');
		cont.disabled = false;
		cont.focus();
	}
}

function update_continue()
{
	_redirect('index.php?what=config');
}

/* end update movie database ajax */